

#!/usr/bin/python

import rospy
# import thread
from pi_driver.srv import GetString, GetInt32, GetStringResponse, GetInt32Response
from std_msgs.msg import String
import json
import nfc
import ndef

class NfcNode:
    def __init__(self):
        self.node_name = rospy.get_name()
        rospy.loginfo("[%s] Initializing......" % (self.node_name))
        self.clf = nfc.ContactlessFrontend('tty:USB0:pn532')

        self.srv_read_nfc_id = rospy.Service(
            "~read_nfc_id", GetString, self.cbReadNfcId)
        rospy.loginfo("[%s] Initialized......" % (self.node_name))

    def cbReadNfcId(self, msg):
        tag = self.clf.connect(rdwr={'on-connect': lambda tag: False})
        return GetStringResponse(tag.identifier.hex())

    def onShutdown(self):
        self.clf.close()
        rospy.loginfo("[%s] shutdown......" % (self.node_name))


if __name__ == '__main__':
    rospy.init_node('nfc_node', anonymous=False)
    node = NfcNode()
    rospy.on_shutdown(node.onShutdown)
    # thread.start_new_thread(node.joy.start_open_loop, ())
    # node.joy.start_open_loop()
    rospy.spin()
